package com.ruoyi.project.module.order.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.module.order.domain.Order;
import com.ruoyi.project.module.order.mapper.OrderMapperPlus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * 订单管理Service业务层处理
 * 
 * @author javen
 * @date 2020-12-20
 */
@Service("orderServicePlus")
public class OrderServicePlusImpl extends OrderServiceImpl{

    @Autowired
    private OrderMapperPlus orderMapper;

    @Value("${ruoyi.profile}")
    private String profile;

    @Autowired
    private ServerConfig serverConfig;

    public Order selectOrderByOutTradeNo(String outTradeNo){
        return orderMapper.selectOrderByOutTradeNo(outTradeNo);
    }

    /**
     * 上传发票
     * @param file
     * @param ajax
     * @throws Exception
     */
    public void uploadInvoice(MultipartFile file, AjaxResult ajax) throws Exception {
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        // 下载地址
        String downloadUrl = serverConfig.getUrl() + fileName;
        ajax.put("invoiceUrl", downloadUrl);
        ajax.put("billingTime", DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", new Date()));
        // 存储地址（服务器本地地址）
        ajax.put("invoicePath", profile + fileName.replaceFirst("/profile", ""));
        ajax.put("isInvoiced", true);
    }

}